﻿Module BingTranslation
    Public Const BingAppID As String = "0112A848809C8084D5FD389951B8210689F115D7"
    Public BingLanguages As System.Data.DataTable = Nothing
    Public BingClient As New BingTranslator.LanguageServiceClient
    Public Sub BingTranslate()
        'Translates text using Bing
        goti.rtbResult.Text = BingClient.Translate(BingAppID, goti.rtbFrom.Text.ToString, goti.LanguageFrom.SelectedValue, goti.LanguageTo.SelectedValue, "text/plain", "general")
    End Sub
    Public Sub LoadBingLanguages()
        Dim Lang1 As String = goti.LanguageFrom.Text
        Dim Lang2 As String = goti.LanguageTo.Text

        If BingLanguages Is Nothing Then
            BingLanguages = New System.Data.DataTable
            BingLanguages.Columns.Add("LanguageName")
            BingLanguages.Columns.Add("LanguageValue")


            For Each Language In BingClient.GetLanguageNames(BingAppID, "en", BingClient.GetLanguagesForTranslate(BingAppID)) '<-- Should this be localized for current user instead?
                BingLanguages.Rows.Add(Language, "")
            Next

            Dim RowIdx As Integer = 0
            For Each LanguageCode In BingClient.GetLanguagesForTranslate(BingAppID)
                BingLanguages.Rows(RowIdx).Item("LanguageValue") = LanguageCode
                RowIdx += 1
            Next
        End If

        With goti.LanguageFrom
            .DataSource = BingLanguages.Copy
            .DisplayMember = "LanguageName"
            .ValueMember = "LanguageValue"
        End With


        With goti.LanguageTo
            .DataSource = BingLanguages.Copy
            .DisplayMember = "LanguageName"
            .ValueMember = "LanguageValue"
        End With

        goti.SelectValueInCombo(goti.LanguageFrom, goti.LanguageFrom.DisplayMember, Lang1)
        goti.SelectValueInCombo(goti.LanguageTo, goti.LanguageTo.DisplayMember, Lang2)
    End Sub
End Module
